package com.example.baselibrary.wigits

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import com.example.baselibrary.R

class PageFlipperView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private var currentPage = 1
    private var totalPages = 10
    private var onPageChangeListener: ((Int) -> Unit)? = null

    private lateinit var leftButton: Button
    private lateinit var rightButton: Button
    private val pageButtons = mutableListOf<Button>()

    init {
        initView()
    }

    private fun initView() {
        orientation = HORIZONTAL
        LayoutInflater.from(context).inflate(R.layout.view_page_flipper, this, true)

        leftButton = findViewById(R.id.btn_left)
        rightButton = findViewById(R.id.btn_right)

        // 初始化页码按钮
        pageButtons.clear()
        pageButtons.add(findViewById(R.id.btn_page_1))
        pageButtons.add(findViewById(R.id.btn_page_2))
        pageButtons.add(findViewById(R.id.btn_page_3))
        pageButtons.add(findViewById(R.id.btn_page_4))
        pageButtons.add(findViewById(R.id.btn_page_5))
        pageButtons.add(findViewById(R.id.btn_page_6))

        // 设置按钮点击事件
        leftButton.setOnClickListener {
            if (currentPage > 1) {
                setCurrentPage(currentPage - 1)
            }
        }

        rightButton.setOnClickListener {
            if (currentPage < totalPages) {
                setCurrentPage(currentPage + 1)
            }
        }

        // 为页码按钮设置点击事件
        for (i in pageButtons.indices) {
            pageButtons[i].setOnClickListener {
                val page = getPageNumberForButton(i)
                if (page <= totalPages) {
                    setCurrentPage(page)
                }
            }
        }

        // 初始化显示
        updateButtons()
    }

    fun setTotalPages(total: Int) {
        totalPages = total
        if (currentPage > totalPages) {
            currentPage = totalPages
        }
        updateButtons()
    }

    fun setCurrentPage(page: Int) {
        if (page in 1..totalPages) {
            currentPage = page
            updateButtons()
            onPageChangeListener?.invoke(currentPage)
        }
    }

    fun getCurrentPage(): Int = currentPage

    fun setOnPageChangeListener(listener: (Int) -> Unit) {
        onPageChangeListener = listener
    }

    private fun updateButtons() {
        // 更新左右按钮状态
        leftButton.isEnabled = currentPage > 1
        rightButton.isEnabled = currentPage < totalPages

        // 计算显示的页码范围
        val startPage = calculateStartPage()

        // 更新页码按钮
        for (i in pageButtons.indices) {
            val pageNumber = startPage + i
            val button = pageButtons[i]

            if (pageNumber <= totalPages) {
                button.text = pageNumber.toString()
                button.visibility = View.VISIBLE

                // 高亮当前页
                if (pageNumber == currentPage) {
                    button.isSelected = true
                    button.setBackgroundResource(R.drawable.page_button_selected)
                } else {
                    button.isSelected = false
                    button.setBackgroundResource(R.drawable.page_button_normal)
                }
            } else {
                button.visibility = View.INVISIBLE
            }
        }
    }

    private fun calculateStartPage(): Int {
        val totalVisiblePages = 6
        var startPage = currentPage - (totalVisiblePages / 2)

        // 调整起始页，确保不超出范围
        if (startPage < 1) {
            startPage = 1
        } else if (startPage + totalVisiblePages - 1 > totalPages) {
            startPage = totalPages - totalVisiblePages + 1
            if (startPage < 1) startPage = 1
        }

        return startPage
    }

    private fun getPageNumberForButton(buttonIndex: Int): Int {
        val startPage = calculateStartPage()
        return startPage + buttonIndex
    }
}
